package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NCementDataRatioBo;
import com.ruoyi.zhgd.domain.vo.NCementDataRatioVo;
import com.ruoyi.zhgd.domain.NCementDataRatio;
import com.ruoyi.zhgd.mapper.NCementDataRatioMapper;
import com.ruoyi.zhgd.service.INCementDataRatioService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import com.baomidou.dynamic.datasource.annotation.DS;

/**
 * 水稳拌合站占比数据Service业务层处理
 *
 * @author zyb
 * @date 2023-09-06
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NCementDataRatioServiceImpl implements INCementDataRatioService {

    private final NCementDataRatioMapper baseMapper;

    /**
     * 查询水稳拌合站占比数据
     */
    @Override
    public NCementDataRatioVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询水稳拌合站占比数据列表
     */
    @Override
    public TableDataInfo<NCementDataRatioVo> queryPageList(NCementDataRatioBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NCementDataRatio> lqw = buildQueryWrapper(bo);
        Page<NCementDataRatioVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询水稳拌合站占比数据列表
     */
    @Override
    public List<NCementDataRatioVo> queryList(NCementDataRatioBo bo) {
        LambdaQueryWrapper<NCementDataRatio> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NCementDataRatio> buildQueryWrapper(NCementDataRatioBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NCementDataRatio> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getGravel1RatioAct() != null, NCementDataRatio::getGravel1RatioAct, bo.getGravel1RatioAct());
        lqw.eq(bo.getGravel2RatioAct() != null, NCementDataRatio::getGravel2RatioAct, bo.getGravel2RatioAct());
        lqw.eq(bo.getGravel3RatioAct() != null, NCementDataRatio::getGravel3RatioAct, bo.getGravel3RatioAct());
        lqw.eq(bo.getGravel4RatioAct() != null, NCementDataRatio::getGravel4RatioAct, bo.getGravel4RatioAct());
        lqw.eq(bo.getGravel5RatioAct() != null, NCementDataRatio::getGravel5RatioAct, bo.getGravel5RatioAct());
        lqw.eq(bo.getGravel6RatioAct() != null, NCementDataRatio::getGravel6RatioAct, bo.getGravel6RatioAct());
        lqw.eq(bo.getPowder1RatioAct() != null, NCementDataRatio::getPowder1RatioAct, bo.getPowder1RatioAct());
        lqw.eq(bo.getPowder2RatioAct() != null, NCementDataRatio::getPowder2RatioAct, bo.getPowder2RatioAct());
        lqw.eq(bo.getWaterRatioAct() != null, NCementDataRatio::getWaterRatioAct, bo.getWaterRatioAct());
        lqw.eq(bo.getGravel1RatioTheo() != null, NCementDataRatio::getGravel1RatioTheo, bo.getGravel1RatioTheo());
        lqw.eq(bo.getGravel2RatioTheo() != null, NCementDataRatio::getGravel2RatioTheo, bo.getGravel2RatioTheo());
        lqw.eq(bo.getGravel3RatioTheo() != null, NCementDataRatio::getGravel3RatioTheo, bo.getGravel3RatioTheo());
        lqw.eq(bo.getGravel4RatioTheo() != null, NCementDataRatio::getGravel4RatioTheo, bo.getGravel4RatioTheo());
        lqw.eq(bo.getGravel5RatioTheo() != null, NCementDataRatio::getGravel5RatioTheo, bo.getGravel5RatioTheo());
        lqw.eq(bo.getGravel6RatioTheo() != null, NCementDataRatio::getGravel6RatioTheo, bo.getGravel6RatioTheo());
        lqw.eq(bo.getPowder1RatioTheo() != null, NCementDataRatio::getPowder1RatioTheo, bo.getPowder1RatioTheo());
        lqw.eq(bo.getPowder2RatioTheo() != null, NCementDataRatio::getPowder2RatioTheo, bo.getPowder2RatioTheo());
        lqw.eq(bo.getWaterRatioTheo() != null, NCementDataRatio::getWaterRatioTheo, bo.getWaterRatioTheo());
        lqw.eq(bo.getGravel1RatioError() != null, NCementDataRatio::getGravel1RatioError, bo.getGravel1RatioError());
        lqw.eq(bo.getGravel2RatioError() != null, NCementDataRatio::getGravel2RatioError, bo.getGravel2RatioError());
        lqw.eq(bo.getGravel3RatioError() != null, NCementDataRatio::getGravel3RatioError, bo.getGravel3RatioError());
        lqw.eq(bo.getGravel4RatioError() != null, NCementDataRatio::getGravel4RatioError, bo.getGravel4RatioError());
        lqw.eq(bo.getGravel5RatioError() != null, NCementDataRatio::getGravel5RatioError, bo.getGravel5RatioError());
        lqw.eq(bo.getGravel6RatioError() != null, NCementDataRatio::getGravel6RatioError, bo.getGravel6RatioError());
        lqw.eq(bo.getPowder1RatioError() != null, NCementDataRatio::getPowder1RatioError, bo.getPowder1RatioError());
        lqw.eq(bo.getPowder2RatioError() != null, NCementDataRatio::getPowder2RatioError, bo.getPowder2RatioError());
        lqw.eq(bo.getWaterRatioError() != null, NCementDataRatio::getWaterRatioError, bo.getWaterRatioError());
        return lqw;
    }

    /**
     * 新增水稳拌合站占比数据
     */
    @Override
    public Boolean insertByBo(NCementDataRatioBo bo) {
        NCementDataRatio add = BeanUtil.toBean(bo, NCementDataRatio.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改水稳拌合站占比数据
     */
    @Override
    public Boolean updateByBo(NCementDataRatioBo bo) {
        NCementDataRatio update = BeanUtil.toBean(bo, NCementDataRatio.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NCementDataRatio entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除水稳拌合站占比数据
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
